home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / mail110 / quit.c < prev    next >
C/C++ Source or Header  |  1994-02-25  |  3KB  |  128 lines

  1. //=========================================================
  2. //
  3. //    quit.c
  4. //
  5. //    void quit(int update)
  6. //
  7. //==========================================================
  8.  
  9. // $Id: quit.c,v 1.3 1994/02/25 13:34:54 gbj Exp user $
  10.  
  11. /*
  12. $Log: quit.c,v $
  13.  * Revision 1.3  1994/02/25  13:34:54  gbj
  14.  * Tidy-up.
  15.  *
  16.  * Revision 1.2  1994/02/08  23:33:54  gbj
  17.  * First public release.
  18.  *
  19.  * Revision 1.1  1994/02/08  03:16:14  gbj
  20.  * Initial revision
  21.  *
  22. */
  23.  
  24. #include "mailer.h"
  25. #include "utils.h"
  26.  
  27. static char buf[4096];
  28.  
  29. void quit(int update)
  30. {
  31.  
  32.     char iname[128], oname[128], *bp;
  33.     FILE *ifd, *ofd;
  34.     int ix, res;
  35.     
  36.     if (!update)            // Don't update mailbox if 'x' exit
  37.         return;
  38.  
  39.     printf("\nUpdating mailbox, please wait...\n");    
  40.     strcpy(iname, mailpath);
  41.     strcat(iname, "\\");
  42.     strcat(iname, user);
  43.     strcpy(oname, iname);
  44.     strcat(iname, ".txt");
  45.     strcat(oname, ".$$$");
  46.     
  47.     ofd=NULL;
  48.     ifd=NULL;
  49.     ofd=fopen(oname, "wb");
  50.     if (ofd == NULL)
  51.     {
  52.         fprintf(stderr, "quit: Can't open %s\n", oname);
  53.         remove(oname);
  54.         return;
  55.     }
  56.     
  57.     // Now update the mailbox, removing deleted messages
  58.     for (ix=0; ix <= maxmsgno; ix++)
  59.     {
  60.         
  61.         if (!mailix[ix].dflag)
  62.         {
  63.             // Not deleted so copy it out
  64.             res=tmp_msg(ix, "quit.$$$", FALSE, TRUE);
  65.             if (res)
  66.             {
  67.                 fprintf(stderr, "quit: Can't update mailbox %s\n",
  68.                     iname);
  69.                 if (ofd)
  70.                     fclose(ofd);
  71.                 remove ("quit.$$$");
  72.                 remove(oname);
  73.                 return;
  74.             }
  75.         
  76.             ifd=fopen("quit.$$$", "rb");
  77.             if (ifd == NULL)
  78.             {
  79.                 fprintf(stderr, "quit: Can't open quit.$$$\n");
  80.                 if (ofd)
  81.                     fclose(ofd);
  82.                 remove("quit.$$$");
  83.                 remove(oname);
  84.                 return;
  85.             }
  86.             // tmp_msg does not copy the initial From ... line so
  87.             // create it now
  88.             sprintf(buf, "From %s %s\n", 
  89.                     mailix[ix].sender, mailix[ix].msgdate);
  90.             fputs(buf, ofd);
  91.             // And put out the new X-Status: line,
  92.             // don't check deleted flag as it can't be set or else
  93.             // I wouldn't be here (famous last words!)
  94.             sprintf(buf, "X-Status: %c \n",(mailix[ix].rflag ? 'Y' : ' '));
  95.             fputs(buf, ofd);
  96.         
  97.             // Now put out the message from quit.$$$
  98.             bp=fgets(buf, 4095, ifd);
  99.             while (bp != NULL)
  100.             {
  101.                 scaneol(buf);
  102.                 strcat(buf, "\n");
  103.                 // Drop old X-Status: line
  104.                 if (strncmp(buf, "X-Status:", 9) != 0)
  105.                     fputs(buf, ofd);
  106.                 bp=fgets(buf, 4095, ifd);
  107.             }
  108.             fclose(ifd);
  109.             ifd=NULL;
  110.             remove("quit.$$$");
  111.         }
  112.     }
  113.     if (ifd)
  114.         fclose(ifd);
  115.     if (ofd)
  116.         fclose(ofd);
  117.     remove("quit.$$$");
  118.     remove(iname);
  119.     res=rename(oname, iname);    // Rename $$$ to txt
  120.     if (res != 0)
  121.     {
  122.         perror("quit");
  123.         fprintf(stderr, "quit: Can't rename %s to %s\n", oname, iname);
  124.         fprintf(stderr, "quit: %s must be manually renamed!\n", oname);
  125.     }
  126.     return;
  127. }
  128.